package me.zhengjie.pm.projectstage.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import me.zhengjie.pm.projectbase.domain.ProjectBase;
import me.zhengjie.pm.projectstageprogress.domain.ProjectStageProgress;
import org.hibernate.annotations.ColumnTransformer;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.io.Serializable;
import java.util.List;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Entity
@Data
@Table(name="project_stage")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ProjectStage implements Serializable {

    /** ID */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    /** 项目ID */
    @Column(name = "project_id")
    private Integer projectId;

//    @ManyToOne(targetEntity =ProjectBase.class,fetch = FetchType.EAGER)
//    @JoinColumn(name = "project_id",referencedColumnName = "id",insertable = false, updatable = false, nullable=true)
    @ManyToOne
    @JoinColumn(name = "project_id",insertable = false, updatable = false, nullable=true)
    private ProjectBase projectBase;

    /** 项目阶段名称 */
    @Column(name = "stage_name")
    private String stageName;

    /** 项目阶段描述 */
    @Column(name = "stage_desc")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(stage_desc), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String stageDesc;

    /** 预期起始时间 */
    @Column(name = "plan_start_time")
    private Timestamp planStartTime;

    /** 预期完成时间 */
    @Column(name = "plan_finish_time")
    private Timestamp planFinishTime;

    /** 实际阶段开始时间 */
    @Column(name = "start_time")
    private Timestamp startTime;

    /** 实际阶段结束时间 */
    @Column(name = "end_time")
    private Timestamp endTime;

    /** 阶段在整个项目占比 */
    @Column(name = "ratio")
    private Double ratio;

    /** 阶段工作完成状态 */
    @Column(name = "work_state")
    private String workState;

    /** 本阶段工作周报列表，由阶段进度汇报表的stage_id字段为外键 */
    @OneToMany(targetEntity =ProjectStageProgress.class, cascade=CascadeType.REFRESH)
    @JoinColumn(name="stage_id")
    private List<ProjectStageProgress> progressList;

    public void copy(ProjectStage source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
}